<?php

namespace app\common\model;

use EasyWeChat\Factory;
use EasyWeChat\Kernel\Messages\TextCard;

/**
 * 企业微信
 */
class Work
{
    private $config = null;
    private $app = null;

    public function __construct()
    {
        $config = [
            'corp_id' => env('work.crop_id'),

            'agent_id' => intval(env('work.agent_id')), // 如果有 agend_id 则填写
            'secret'   => env('work.secret'),

            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',

            'log' => [
                'level' => 'debug',
                'file' => root_path().'runtime/work/wechat.log',
            ],
        ];
        $this->app = Factory::work($config);
    }

    /**
     * 系统报错通知
     */
    public function exceptionNotify(string $message)
    {
        $message = '惠充系统报错提醒'.PHP_EOL.$message;
        $this->app->messenger->toUser('null')->send($message);
    }

    /**
     * 供应商余额不足提醒
     */
    public function balanceWarning($message,$ids)
    {
        $this->app->messenger->toUser($ids)->send($message);
    }

    /**
     *  获取部门人员
     */
    public function showDepartmentUsers(){
//         $res = $this->app->department->list();
         $departmentId = 1;
         $res = $this->app->user->getDepartmentUsers($departmentId);
         $list=$res['userlist'] ?? [];
         return $list;
    }
}
